<template>
    <div id="parts">
        <div class="operation">
            <el-button v-if="menuId==='4571'" @click="insertTask" class="slot-btn-top" type="primary" size="small">发放任务</el-button>
            <!-- 查询 -->
            <el-popover placement="bottom-start" width="1000" style="box-shadow: 0 0 8px rgba(0,0,0,.5);" trigger="click">
                <el-form :inline="true" :model="search">
                    <el-form-item label="新机器条码" >
                        <el-input v-model="search.code"></el-input>
                    </el-form-item>
                    <el-form-item label="测试类型" >
                        <el-autocomplete  class="inputSuffix"  v-model="search.name" :fetch-suggestions="searchAsyncSupplier" placeholder="请输入供应商名称" @select="changeSupplier"></el-autocomplete>
                    </el-form-item>

                    <el-button type="primary" @click="searchBtn" icon="el-icon-search">查询</el-button>
                </el-form>
                <el-button slot="reference" type="primary">查询</el-button>
            </el-popover>
        </div>
        <el-table :data="dataList" stripe border class="table-index" :height="tableHeight" style="width: 100%;" :header-cell-style="getRowClass" v-loading="loading">
            <el-table-column fixed type="index" label="*" sortable min-width="40"></el-table-column>
            <el-table-column prop="taskcode" label="新机器条码" sortable min-width="160"></el-table-column>
            <el-table-column prop="batchnums" label="生产批号" sortable min-width="160"></el-table-column>
            <el-table-column prop="resourcenamename" label="测试类型" sortable min-width="130"></el-table-column>
            <el-table-column prop="typenums" label="产品型号" sortable min-width="110"></el-table-column>
            <el-table-column prop="taskreleaseaddtime" label="任务创建时间" sortable min-width="160"></el-table-column>
            <el-table-column prop="taskstartorendtime" label="任务需完成时间" sortable min-width="160"></el-table-column>
            <el-table-column prop="istaskstart" label="任务是否接收" :formatter="formatNeedTrain" sortable min-width="160"></el-table-column>
            <el-table-column prop="taskbegintime" label="开始启动时间" sortable min-width="160"></el-table-column>
            <el-table-column prop="taskendtime" label="实际结束时间" sortable min-width="160"></el-table-column>
            <el-table-column fixed="right" label="操作" sortable align="center" width="75">
                <template slot-scope="scope">
                    <el-button @click="updateList(scope.row)" type="text" size="small">编辑</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pagination.currentPage" :page-sizes="[5,10, 15,20, 25,30,50, 100]" :page-size="pagination.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pagination.total"></el-pagination>
        <!-- 详情信息 -->
        <el-dialog :title="dialogTitle" width="85%" top="30px" :visible.sync="isDialog" :close-on-click-modal="false" >
            <el-tabs v-model="activeName" @tab-click="handleClick" style="padding-bottom: 15px;">
                <el-tab-pane label="任务信息" name="1" class="dialog1" :style="dialogHeightPane" >
                    <h3 class="dialog-plate-title"><span>基本信息</span></h3>
                    <el-form :inline="true" class="demo-form-inline" v-model="dialogList" label-position="right" label-width="180px">
                        <el-form-item label="新机器条码"><el-input v-model="dialogList.taskcode" :disabled="!dialogListDisabled.taskcode"></el-input></el-form-item>
                        <el-form-item label="生产批号"><el-input v-model="dialogList.batchnums" :disabled="!dialogListDisabled.batchnums"></el-input></el-form-item>
                        <el-form-item label="测试类型"><el-input  v-model="dialogList.resourcenamename" :disabled="!dialogListDisabled.resourcenamename"></el-input></el-form-item>
                        <el-form-item label="产品型号"><el-input v-model="dialogList.typenums" :disabled="!dialogListDisabled.typenums"></el-input></el-form-item>
                        <el-form-item label="业务类型"><el-input v-model="dialogList.businesstype" :disabled="!dialogListDisabled.businesstype"></el-input></el-form-item>
                        <el-form-item label="任务创建人"><el-input v-model="dialogList.taskreleasepersonName" :disabled="!dialogListDisabled.taskreleasepersonName"></el-input></el-form-item>
                        <el-form-item label="任务创建时间"><el-date-picker v-model="dialogList.taskreleaseaddtime" :disabled="!dialogListDisabled.taskreleaseaddtime" format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择日期时间" style="width: 200px;"></el-date-picker></el-form-item>
                        <el-form-item label="任务负责人">
                            <el-select v-model="dialogList.taskperformperson" :disabled="!dialogListDisabled.taskperformperson">
                                <el-option v-for="item in materialManagers" :key="item.id" :label="item.name" :value="item.id"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="任务需完成时间">
                            <el-date-picker v-model="dialogList.taskstartorendtime" :disabled="!dialogListDisabled.taskstartorendtime" format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择日期时间" style="width: 200px;"></el-date-picker>
                        </el-form-item>
                        <el-form-item label="计划创建类型">
                            <el-select v-model="dialogList.tasktype" :disabled="!dialogListDisabled.tasktype">
                                <el-option label="计划任务" :value="1"></el-option>
                                <el-option label="临时任务" :value="2"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-form>

                    <h3 class="dialog-plate-title"><span>待办信息</span></h3>
                    <el-form :inline="true" class="demo-form-inline" v-model="dialogList" label-position="right" label-width="180px">
                        <el-form-item label="任务是否接收">
                            <el-select v-model="dialogList.istaskstart" :disabled="!dialogListDisabled.istaskstart">
                                <el-option label="否" value="0"></el-option>
                                <el-option label="是" value="1"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="接收创建时间">
                            <el-date-picker v-model="dialogList.taskstartaddtime" :disabled="!dialogListDisabled.taskstartaddtime" format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择日期时间" style="width: 200px;"></el-date-picker>
                        </el-form-item>
                    </el-form>

                    <h3 class="dialog-plate-title"><span>进行中信息</span></h3>
                    <el-form :inline="true" class="demo-form-inline" v-model="dialogList" label-position="right" label-width="180px">
                        <el-form-item label="任务是否开始">
                            <el-select v-model="dialogList.istaskbegin" :disabled="!dialogListDisabled.istaskbegin">
                                <el-option label="否" value="0"></el-option>
                                <el-option label="是" value="1"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="任务开始时间">
                            <el-date-picker v-model="dialogList.taskbegintime" :disabled="!dialogListDisabled.taskbegintime" format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择日期时间" style="width: 200px;"></el-date-picker>
                        </el-form-item>
                        <el-form-item label="启动创建时间">
                            <el-date-picker v-model="dialogList.taskbeginaddtime" :disabled="!dialogListDisabled.taskbeginaddtime" format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择日期时间" style="width: 200px;"></el-date-picker>
                        </el-form-item>
                        <el-form-item label="任务是否结束">
                            <el-select v-model="dialogList.istaskend" :disabled="!dialogListDisabled.istaskend">
                                <el-option label="否" value="0"></el-option>
                                <el-option label="是" value="1"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="实际结束时间">
                            <el-date-picker v-model="dialogList.taskendtime" :disabled="!dialogListDisabled.taskendtime" format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择日期时间" style="width: 200px;"></el-date-picker>
                        </el-form-item>
                        <el-form-item label="结束创建时间">
                            <el-date-picker v-model="dialogList.taskendaddtime" :disabled="!dialogListDisabled.taskendaddtime" format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择日期时间" style="width: 200px;"></el-date-picker>
                        </el-form-item>
                    </el-form>

                    <h3 class="dialog-plate-title"><span>复核信息</span></h3>
                    <el-form :inline="true" class="demo-form-inline" v-model="dialogList" label-position="right" label-width="180px">
                        <el-form-item label="任务是否复核">
                            <el-select v-model="dialogList.isreviewtask" :disabled="!dialogListDisabled.isreviewtask">
                                <el-option label="否" value="0"></el-option>
                                <el-option label="是" value="1"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="复核执行人">
                            <el-input v-model="dialogList.reviewpersonName" :disabled="!dialogListDisabled.reviewpersonName"></el-input>
                        </el-form-item>
                        <el-form-item label="复核结果">
                            <el-select v-model="dialogList.reviewresult" :disabled="!dialogListDisabled.reviewresult">
                                <el-option label="【空】" value="-1"></el-option>
                                <el-option label="正常" value="1"></el-option>
                                <el-option label="异常" value="0"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="复核时间">
                            <el-date-picker v-model="dialogList.reviewtime" :disabled="!dialogListDisabled.reviewtime" format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择日期时间" style="width: 200px;"></el-date-picker>
                        </el-form-item>
                        <el-form-item label="复核创建时间">
                            <el-date-picker v-model="dialogList.reviewaddtime" :disabled="!dialogListDisabled.reviewaddtime" format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择日期时间" style="width: 200px;"></el-date-picker>
                        </el-form-item>
                    </el-form>
                    <div style="text-align: right">
                        <el-button @click="submitList" type="primary">确 定</el-button>
                    </div>
                </el-tab-pane>
                <el-tab-pane  label="过程状态信息" name="2" :style="dialogHeightPane">
                    <check-form :data="process.list" @submitHanld="submitHanld" @changeDisable="changeDisable" :isDisabled="process.disabled"></check-form>
                </el-tab-pane>
                <el-tab-pane   label="技术标准" name="3" :style="dialogHeightPane">
                    <el-table  :data="technicalStandard" tooltip-effect="dark"  style="width: 100%" >
                        <el-table-column prop="fileName" label="文件名称"> </el-table-column>
                        <el-table-column fixed="right" label="操作" sortable align="center" width="160">
                            <template slot-scope="scope">
                                <el-button @click="showDialog1('watch',scope.row)" type="text" size="small">查看</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-tab-pane>
                <el-tab-pane  label="图纸" name="4" :style="dialogHeightPane">
                    <el-table  :data="drawingList" tooltip-effect="dark"  style="width: 100%" >
                        <el-table-column prop="cidName" label="图纸名称"> </el-table-column>
                         <el-table-column fixed="right" label="操作" sortable align="center" width="160">
                            <template slot-scope="scope">
                                <el-button @click="showDialog1('watch',scope.row)" type="text" size="small">查看</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-tab-pane>
                <el-tab-pane  label="SOP" name="5" :style="dialogHeightPane">
                    <el-table  :data="sopList" tooltip-effect="dark"  style="width: 100%" >
                        <el-table-column prop="fileName" label="SOP名称"> </el-table-column>
                        <el-table-column fixed="right" label="操作" sortable align="center" width="160">
                            <template slot-scope="scope">
                                <el-button @click="showDialog1('watch',scope.row)" type="text" size="small">查看</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-tab-pane>
            </el-tabs>
        </el-dialog>

        <el-dialog :title="taskDialg.title" width="600px" center :visible.sync="taskDialg.isShow" :close-on-click-modal="false" style="padding-bottom: 15px;">
            <el-form :inline="true" class="demo-form-inline" v-model="taskDialg.formData" label-position="right" label-width="180px">
                <el-form-item label="机器编号"><el-input v-model="taskDialg.formData.resourcename" ></el-input></el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer" style="text-align: right;">
                <el-button @click="addTask" type="primary">确 定</el-button>
            </div>
        </el-dialog>
          <el-dialog :title="dialog1.title" width="90%" top="30px" :visible.sync="dialog1.isShow" :close-on-click-modal="false" v-dialogDrag>
            <div v-if="activeName==3||activeName==5">
                <div v-html="dialog1.content"></div>
            </div>
            <div v-if="activeName==4">
                <el-input v-model.number="drawingPdf.instructionsPageApply" type="number" style="width: 100px;" min="1"
                    :max="drawingPdf.instructionsNumPagesApply"></el-input>
                <span>/{{drawingPdf.instructionsNumPagesApply}}</span>
                <el-button @click="drawingPdf.instructionsRotateApply += 90">&#x27F3;</el-button>
                <pdf ref="applyInstructionsPdf"
                        style="border: 1px solid red;"
                        :src="drawingPdf.src"
                        :page="drawingPdf.instructionsPageApply"
                        :rotate="drawingPdf.instructionsRotateApply"
                        @progress="drawingPdf.instructionsLoadedRatioApply = $event"
                        @num-pages="drawingPdf.instructionsNumPagesApply = $event"
                        @link-clicked="drawingPdf.instructionsPageApply = $event">
                </pdf>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import pdf from 'vue-pdf';
import machineHttp from '@/axios/machine.js'
import checkForm from './components/checkForm.vue'
    export default {
        name: "",
        components:{
            pdf,
            checkForm
        },
        data() {
            let token = sessionStorage.getItem("tokenId");
            let menuId = sessionStorage.getItem('menuId');
            return {
                tokenId: token,
                menuId:menuId,
                search:{
                  code:'',
                  name:'',
                  nameId:''
                },
                pagination:{
                    currentPage:1,
                    pageSize:10,
                    total:0
                },
                activeName: '1',
                materialManagers:[],
                tableHeight: document.documentElement.clientHeight - 135,
                dataList: [],
                loading:false,
                //基本信息
                isDialog: false,
                dialogTitle: '',
                dialogHeightPane: {
                    height: (document.documentElement.clientHeight - 180) + 'px',
                    overflowY: 'auto'
                },
                dialogTableHeight: document.documentElement.clientHeight - 260,
                dialogList:{
                    actualcompletenums: "",
                    batchnums: "",
                    businesstype: "",
                    isreviewtask: "",
                    istaskbegin: "",
                    istaskend: "",
                    istaskstart: "",
                    resourcename: "",
                    resourcenamename: "",
                    reviewaddtime: "",
                    reviewperson: "",
                    reviewpersonName: "",
                    reviewresult: "",
                    reviewtime: "",
                    taskbeginaddtime: "",
                    taskbegintime: "",
                    taskcode: "",
                    taskendaddtime: "",
                    taskendtime: "",
                    taskfinishstate: "",
                    tasknums: "",
                    taskperformperson: "",
                    taskperformpersonName: "",
                    taskreleaseaddtime: "",
                    taskreleaseperson: "",
                    taskreleasepersonName: "",
                    taskstartaddtime: "",
                    taskstartorendtime: "",
                    taskstate: "",
                    tasktype: "",
                    tid: "",
                    typenums: "",
                },
                dialogListDisabled:{
                    taskcode: true,
                    batchnums: true,
                    resourcename: true,
                    resourcenamename: true,
                    typenums: true,
                    businesstype: true,
                    taskreleaseperson: true,
                    taskreleasepersonName: true,
                    taskreleaseaddtime: true,
                    taskperformperson: true,
                    taskperformpersonName: true,
                    taskstartorendtime: true,
                    tasktype: true,
                    istaskstart: true,
                    taskstartaddtime: true,
                    istaskbegin: true,
                    taskbegintime: true,
                    taskbeginaddtime: true,
                    istaskend: true,
                    taskendtime: true,
                    taskendaddtime: true,
                    isreviewtask: true,
                    reviewperson: true,
                    reviewpersonName: true,
                    reviewresult: true,
                    reviewtime: true,
                    reviewaddtime: true,

                    actualcompletenums: true,
                    taskfinishstate: true,
                    tasknums: true,
                    taskstate: true,
                    tid: true,
                },
                taskDialg:{
                    title:'',
                    isShow:false,
                    formData:{}
                },
                dialog1:{
                     title:'',
                    isShow:false,
                    content:''
                },
                process:{
                    list:[],
                    disabled:true
                },
                technicalStandard:[],/**技术标准 */
                drawingList:[],/**图纸列表 */
                sopList:[],/**sop列表 */
                /** pdf*/
                drawingPdf:{
                    src:'',
                    instructionsPageApply:1,
                    instructionsNumPagesApply: 0,
                    instructionsRotateApply: 0,
                },
                allOldProcedureList:[]
            };
        },
        created() {
            let self = this;
            this.getData();
            this.getMaterialManagersData();
            machineHttp.allOldProcedureList().then(res=>{
                res.content.forEach(el=>{
                    el.value=el.name
                })
                self.allOldProcedureList = res.content;
            })
        },
        methods: {
            getMaterialManagersData() {/**获取任务负责人姓名 */
                let self = this;
                machineHttp.jqcsMachineTestTaskPerson().then(res=>{
                    self.materialManagers = res.content;
                })
            },
            getData() {
                let self = this;
                self.loading=true;
                let postData={
                        code:self.search.code,
                        name:self.search.name===''?null:self.search.nameId,
                        currentPage:self.pagination.currentPage,
                        pageSize:self.pagination.pageSize
                    }
                switch(self.menuId){
                    case '4571':
                        postData.taskState=0;
                        postData.taskFinishState=0;
                        postData.isReviewTask=0;
                        break;
                    case '4572':
                        postData.taskState=1;
                        postData.taskFinishState=0;
                        postData.isReviewTask=0;
                        break;
                    case '4573':
                        postData.taskState=1;
                        postData.taskFinishState=1;
                        postData.isReviewTask=0;
                        break;
                    case '4574':
                        postData.taskState=1;
                        postData.taskFinishState=1;
                        postData.isReviewTask=1;
                        break;
                }
                machineHttp.jqcsMachineTestList(postData).then(res=>{
                    self.loading=false;
                   self.dataList = res.content.records;
                   self.pagination.total = res.content.total;
                },error=>{
                    self.loading=false;
                })
            },
            searchBtn(){
                this.getData();
            },
            updateList(row) {
                this.isDialog = true;
                this.activeName = '1';
                this.dialogListDisabled={
                    taskcode:false,
                    batchnums: this.menuId==='4571' ? true : false,
                    resourcename: false,
                    resourcenamename: false,
                    typenums:this.menuId==='4571' ? true : false,
                    businesstype:this.menuId==='4571' ? true : false,
                    taskreleaseperson:false,
                    taskreleasepersonName:false,
                    taskreleaseaddtime:false,
                    taskperformperson:this.menuId==='4571' ? true : false,
                    taskperformpersonName:this.menuId==='4571' ? true : false,
                    taskstartorendtime:this.menuId==='4571' ? true : false,
                    tasktype:this.menuId==='4571' ? true : false,
                    istaskstart:this.menuId==='4571' ? true : false,
                    taskstartaddtime:false,
                    istaskbegin:this.menuId==='4572' ? true : false,
                    taskbegintime:this.menuId==='4572' ? true : false,
                    taskbeginaddtime:false,
                    istaskend:this.menuId==='4572' ? true : false,
                    taskendtime:this.menuId==='4572' ? true : false,
                    taskendaddtime:false,
                    isreviewtask:this.menuId==='4573' ? true : false,
                    reviewperson:false,
                    reviewpersonName:false,
                    reviewresult:this.menuId==='4573' ? true : false,
                    reviewtime:this.menuId==='4573' ? true : false,
                    reviewaddtime:false,
                    tasknums:false,
                    actualcompletenums:false,
                    taskfinishstate:false,
                    taskstate:false,
                    tid:false,
                };
                this.dialogList = row;
                this.dialogList.taskperformperson = this.dialogList.taskperformperson ? parseInt(this.dialogList.taskperformperson) : "";
            },
            submitList(){
                let self = this;
                self.dialogList.taskstartorendtime = self.timestampToTime( self.dialogList.taskstartorendtime);
                self.dialogList.taskbegintime = self.timestampToTime( self.dialogList.taskbegintime);
                self.dialogList.taskendtime = self.timestampToTime( self.dialogList.taskendtime);
                self.dialogList.reviewtime = self.timestampToTime( self.dialogList.reviewtime);
                machineHttp.updateJqcsMachineTest(self.dialogList).then(res=>{
                    self.isDialog=false;
                    self.getData();
                    let message='';
                    self.$message.success('任务信息修改成功');
                })
            },
            handleClick(tab, event) {
                let self = this;
                switch(tab.name){
                    case '2':
                        self.getProecssList();
                        break;
                    case '3':
                        self.getTec();
                        break;
                    case '4':
                        self.getDrawingList();
                        break;
                    case '5':
                        self.getSOPList();
                        break;
                }
            },
            handleSizeChange: function (size) {
                this.pagination.pageSize = size;
                this.getData()
            },
            handleCurrentChange: function (currentPage) {
                this.pagination.currentPage = currentPage;
                this.getData()
            },
            insertTask(){
                let self = this;
                self.taskDialg.title='发放任务';
                self.taskDialg.isShow=true;
            },
            getProecssList(){
                let self = this;
                machineHttp.jqcsMachineTestProecssList(self.dialogList.tid).then(res=>{
                    self.process.list = res.content;
                })
            },
            addTask(){
                let self = this;
                let postData={
                    resourcename:self.taskDialg.formData.resourcename
                }
               machineHttp.addJqcsMachineTest(postData).then(res=>{
                   self.$message.success("发放任务成功");
                  self.taskDialg.isShow=false;
                  self.getData();
               })
            },
            submitHanld(data){
                let self = this;
                 let loading =self.$loading({
                        lock: true,
                        text: '数据提交中',
                        spinner: 'el-icon-loading',
                        background: 'rgba(0, 0, 0, 0.7)'
                    });
                    debugger;
                machineHttp.updateJqcsMachineTestProecss(data).then(res=>{
                    loading.close();
                    self.$message.success("过程状态录入成功！");
                    self.getProecssList();
                    self.process.disabled = true;
                },error=>{
                    loading.close();
                    self.$message.error("装配过程检测信息录入失败！");
                })
            },
            changeDisable(data){
                 let self = this;
                self.process.disabled=false;
            },
             getTec(){/**获取技术标准列表 */
                let self=this;
                machineHttp.selectSjfile(self.dialogList.resourcename).then(res=>{
                self.technicalStandard=res.content;
                })
            },
            getDrawingList(){/**获取图纸列表 */
                let self=this;
                machineHttp.jqcsSettingDrawsSelect(self.dialogList.resourcename).then(res=>{
                    self.drawingList=res.content;
                })
            },
            getSOPList(){/**获取sop列表 */
                let self=this;
                machineHttp.jqcsSettingsSopSelect(self.dialogList.resourcename).then(res=>{
                    self.sopList=res.content;
                })
            },
            showDialog1(type,row){
                let self = this;
                self.dialog1.isShow=true;
                switch(self.activeName){
                    case '3':
                    case '5':
                        self.dialog1.title=row.fileName;
                        self.dialog1.content=row.initialContent;
                    break;
                    case '4':
                        self.dialog1.title=row.drawingName;
                        self.drawingPdf.src=`http://192.168.0.93:8889/adscm/zltx/jyDrawingList/viewDrawingPdf/${row.id}/1`;
                        self.drawingPdf.instructionsRotateApply=0;
                    break;
                }
            },
            searchAsyncSupplier(queryString, callback){
                let results = queryString ?  this.allOldProcedureList.filter(this.createStateFilter(queryString)) : this.allOldProcedureList;
                callback(results);
            },
            changeSupplier(item){
                this.search.nameId = item.id;
                this.search.name = item.value;
            },
            createStateFilter(queryString) {
                return (state) => {
                return (state.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
                };
            }
        }
    }
</script>

<style scoped>
</style>
